class util {
  formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  /**
   * 转换时间
   */
  myTime(date) {
    var arr = date.split("T");
    var d = arr[0];
    var darr = d.split('-');
    var t = arr[1];
    var tarr = t.split('.000');
    var marr = tarr[0].split(':');
    var dd = parseInt(darr[0]) + "-" + parseInt(darr[1]) + "-" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]);
    return dd;
  }
  getgoodstype(state) {
    if (state == 'PAYSUCCESS') return '待发货';
    if (state == 'NO_PAY') return '未支付';
    if (state == 'SHIPPED') return '已发货';
    if (state == 'RECEIVED') return '已收货';
    return '';
  }

//   countdowndata(go_time){
//         var now_time=new Date();
//         var alltime =go_time -now_time.getTime ();  //总的时间（毫秒）
//        var haoscend =alltime%1000;  //毫秒
//         //console.log(haoscend);
//         var scend = parseInt ((alltime/1000)%60  ) ;  //秒
//         //console.log(scend);
//         var minute =parseInt((alltime/1000/60)%60  ) ;  //  分钟
//        // console.log(minute);
//         var hour =parseInt((alltime/1000/60/60)%24 ) ;   //小时
//        // console.log(hour);
//         var day=parseInt((alltime/1000/60/60/24)%30);   //天数
//        // console.log(day);
//         var month=parseInt((alltime/1000/60/60/24/30)%12); //月
//        // console.log(month);
//         // var btime=document.getElementById("block");
//         // var time1=document.getElementById("shi_jian");
//         let countdowndata =month+"月"+day+"天"+hour+"时"+minute +"分"+scend +"秒"+(haoscend<10 ?"00"+haoscend :haoscend <100?"0"+haoscend :haoscend );
//       return countdowndata;
// }
// timeFormat(param){//小于10的格式化函数
//   return param < 10 ? '0' + param : param;
//   }
// countdowndata(endTimeList){
//     let newTime = new Date().getTime();
//     let countDownArr = [];
//     // 对结束时间进行处理渲染到页面
//     endTimeList.forEach(x => {
//     let endTime = new Date(x).getTime();
//     let obj = null;
//     // 如果活动未结束，对时间进行处理
//     if (endTime - newTime > 0){
//     let time = (endTime - newTime) / 1000;
//     // 获取天、时、分、秒
//     let day = parseInt(time / (60 * 60 * 24));
//     let hou = parseInt(time % (60 * 60 * 24) / 3600);
//     let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
//     let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
//     obj = {
//     day: this.timeFormat(day),
//     hou: this.timeFormat(hou),
//     min: this.timeFormat(min),
//     sec: this.timeFormat(sec)
//     }
//     }else{//活动已结束，全部设置为'00'
//     obj = {
//     day: '00',
//     hou: '00',
//     min: '00',
//     sec: '00'
//     }
//     }
//     countDownArr.push(obj);
//     })
//     return countDownArr;
// }

  formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-') + ' ' + [hour, minute, second].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join(':')
  }

  formatTimenosecend = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()

    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-') + ' ' + [hour, minute].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join(':')
  }

  


  formatDate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(x => {
      x = x.toString()
      return x[1] ? x : '0' + x
    }).join('-')
  }
  todate(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  }
  toDate(number) {
    var n = number;
    console.log(number, "number");
    var date = new Date(parseInt(n) * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  }

  toDate1(number) {
    var n = number;
    console.log(number, "number");
    var date = new Date(parseInt(n) * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    // minute = minute < 10 ? ('0' + minute) : minute;
    // second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d +' '+h+':'+minute;
  }

  //当前日期加减天数
  mathChangeDate(date, method, days) {
    //method:'+' || '-'
    //ios不解析带'-'的日期格式，要转成'/'，不然Nan，切记
    // var dateVal = date.replace(/-/g, '/');
    var timestamp = Date.parse(date);
    if (method == '+') {
      timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
    } else if (method == '-') {
      timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
    }
    return timestamp;
  }

  decodeurl(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
  }

nocancelmodal(content,title,confirm,cancel){
  wx.showModal({
    title:title||"提示",
    content,
    showCancel:!1,
    success:res=>{
      if(res.confirm) confirm&&confirm()
      if (res.cancel) cancel && cancel()
    }
  })
}
  modal(content, title, confirm, cancel) {
    wx.showModal({
      title,
      content,
      succ: res => {
        if (res.confirm) confirm && confirm()
        if (res.cancel) cancel && cancel()
      }
    })
  }

  setwatcher(data, watch) {
    Object.keys(watch).forEach(x => {
      this.observer(data, x, watch[x])
    })
  }

  observer(obj, key, watchFun) {
    var val = obj[key]; // 给该属性设默认值
    Object.defineProperty(obj, key, {
      configurable: true,
      enumerable: true,
      set: function (value) {
        val = value;
        watchFun(value, val); // 赋值(set)时，调用对应函数
      },
      get: function () {
        return val;
      }
    })
  }

}

module.exports = util

// module.exports = {
//   formatTime: formatTime
// }
